package com.jj.lib;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.jj.adapter.BaseRecyclerAdapter;
import com.jj.base.BaseActivity;
import com.jj.base.ToolBarActivity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qmsoft05 on 2017/3/9.
 */

public class BActvivty extends BaseActivity {
    private RecyclerView rv_b;
    private BAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_b;
    }

    @Override
    protected void initView() {
        rv_b = (RecyclerView) findViewById(R.id.rv_b);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv_b.setLayoutManager(linearLayoutManager);
        adapter = new BAdapter(this, BaseRecyclerAdapter.NEITHER);
        rv_b.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        adapter.addAll(scanLocalInstallAppList());
    }

    private ArrayList<AppModel> scanLocalInstallAppList() {
        ArrayList<AppModel> myAppInfos = new ArrayList<AppModel>();
        try {
            List<PackageInfo> packageInfos = getPackageManager().getInstalledPackages(0);
            for (int i = 0; i < packageInfos.size(); i++) {
                PackageInfo packageInfo = packageInfos.get(i);
                //过滤掉系统app
            if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) {
                continue;
            }
                AppModel myAppInfo = new AppModel();
                myAppInfo.setPackages(packageInfo.packageName);
                if (packageInfo.applicationInfo.loadIcon(getPackageManager()) == null) {
                    continue;
                }
                myAppInfo.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));
                myAppInfo.setTitle(packageInfo.applicationInfo.loadLabel(getPackageManager()).toString());
                myAppInfos.add(myAppInfo);
            }
        }catch (Exception e){
        }
        return myAppInfos;
    }

}
